//根据id获取控件
function GetObject(id) 
{
	if (typeof(document.getElementById) != "undefined") 
	{
		return document.getElementById(id);
	} 
	else 
	{
		return document.all[id];
	}
}
//全选checkbox
function DoSelectAll(theBox, theName)
{
	xState = theBox.checked;
	elm = theBox.form.elements;
  	for (i = 0; i < elm.length; i++) 
  	{
		if (("checkbox" == elm[i].type) && (-1 != elm[i].name.indexOf(theName)))
		{
			elm[i].checked = xState;
		}
	}
}
//判断是否最少选择一项checkbox
function DoCheckSelect(theBox, theName)
{
	elm = theBox.form.elements;
  	for (i = 0; i < elm.length; i++) 
  	{
		if (("checkbox" == elm[i].type) && (-1 != elm[i].name.indexOf(theName)) && (true == elm[i].checked))
		{
			return true;
		}
	}
	return false;
}

//判断获取checkbox系列的值
function GetCheckValue(theBox, theName)
{
	elm = theBox.form.elements;
	val = '';
  	for (i = 0; i < elm.length; i++) 
  	{
		if (("checkbox" == elm[i].type) && (-1 != elm[i].name.indexOf(theName)) && (true == elm[i].checked))
		{
			val += elm[i].value + '_';
		}
	}
	if ('' != val)
		val = val.substr(0, val.length - 1);
	
	return val;
}

//判断获取checkbox系列的值，包括选中的和不选中的
function GetCheckBoxValue(theBox, theName)
{
	elm = theBox.form.elements;
	val = '';
  	for (i = 0; i < elm.length; i++) 
  	{
		if (("checkbox" == elm[i].type) && (-1 != elm[i].name.indexOf(theName)))
		{
			val += elm[i].value + '_';
		}
	}
	if ('' != val)
		val = val.substr(0, val.length - 1);
	
	return val;
}

//键盘控制
function CheckNumber()
{
	var val=window.event.keyCode;
	if ( val < 48 || val > 57)
		return false;
}

//根据theValue的值设置SELECT中的选项
function DoSetSelect(theObj, theValue)
{
	var obj = GetObject(theObj);
	
	var str_value = ",," + theValue + ",";
	
	for (var i = 0; i < obj.length; i++)
	{
		var now_value = "," + obj.options[i].value + ",";
		
		if (0 < str_value.indexOf(now_value))
			obj.options[i].selected = true;
	}
}

//Ajax Delete
function DoDelete(id, type, callback)
{
	if ( !id )
	{
		alert('没有任何记录被选中!');
		return false;
	}
	if ( confirm('确定删除选中的记录吗?') )
	{
		var type = type==undefined ? 'album' : type;
		var callback = callback==undefined ? function(e){
			var r = eval('(' + e.responseText + ')');
			if ( r.error == 0 )
			{
				window.location.reload();
			}
			else
			{
				alert(r.error);
			}
		} : callback;
		new Ajax.Request( 
				'/admin/ajax/delete.php',
				{
					parameters : 'id=' + id + '&type=' + type + '&rs=' + Math.random(),
					method : 'get',
					onSuccess : callback
				}
		);
	}
}
